package com.approval.approvalapp.index.dbsx.wzsp.fragment;


import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.NewNewWzSpBean;
import com.approval.approvalapp.bean.WzSpBean;
import com.approval.approvalapp.bean.WzSpDbBean;
import com.approval.approvalapp.index.dbsx.wzsp.activity.WzSpActivity;
import com.approval.approvalapp.index.dbsx.wzsp.adapter.WzSpDbAdapter;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseFragment;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.view.CustomTouchSwipeRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;


/**
 * 不分页
 * A simple {@link Fragment} subclass.
 */
public class WzSpDbFragment extends BaseFragment {


    @Bind(R.id.xrv)
    XRecyclerView xrv;
    @Bind(R.id.swipeRefreshLayout)
    CustomTouchSwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.empty_ll)
    LinearLayout em;
    private int pagenum = 1;
    private List<WzSpDbBean.StayBean> stayList = new ArrayList<>();
    private WzSpDbAdapter wzSpDbAdapter;
    public static WzSpDbFragment instance;

    @Override
    protected int getLayoutId() {
        instance = this;
        return R.layout.fragment_wz_sp_db;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        instance = null;
    }

    @Override
    public void initView(View view) {
        xrv.setPullRefreshEnabled(false);
        xrv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        wzSpDbAdapter = new WzSpDbAdapter();
        xrv.setAdapter(wzSpDbAdapter);

        swipeRefreshLayout.setRefreshing(true);

        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshData();
            }
        }, 500);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xrv.setNoMore(false);
                        swipeRefreshLayout.setRefreshing(true);
                        refreshData();

                    }
                }, 500);
            }
        });

        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                swipeRefreshLayout.setRefreshing(false);
                xrv.setNoMore(true);
            }
        });
    }

    public void refreshData() {
        pagenum = 1;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("if_use", "0");
        jsonObject.put("region_code", SpUtil.getString(this.getActivity(), SpUtil.REGION_CODE, ""));
        jsonObject.put("org_code", SpUtil.getString(this.getActivity(), SpUtil.ORG_CODE, ""));
        if (SpUtil.getString(this.getActivity(),
                SpUtil.Usertype,"").equals("1")){
            jsonObject.put("is_select", "1");
        }else {
            jsonObject.put("is_select", "0");
        }
        String url1 = "v1/center/material_already";
        String url2 = "v1/center/material_already_g";
        String s;
        if (WzSpActivity.instance.uriid.equals("4")) {
            s = url2;
        }else {
            s = url1;
        }
        Log.d("WzSpDbFragmenaaat", "jsonObject:" + jsonObject + s);
        NetRequsetUtil.getInstance().netRequestPostJson(s, jsonObject.toString(), new NetDownResponse() {

            @Override
            public void success(String str) {

                List<NewNewWzSpBean> stay = JSON.parseArray(str, NewNewWzSpBean.class);

                if (stay == null || stay.size() == 0){
                    em.setVisibility(View.VISIBLE);
                }else {
                    em.setVisibility(View.GONE);
                }

                wzSpDbAdapter.setStayList(stay);
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
            }

            @Override
            public void finish() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void initData() {

    }

}
